package org.csu.mypetstore.aspectJ;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.csu.mypetstore.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

@Aspect
@Configuration
public class AspectJ {
    @Autowired
    LogService logService;

    //切入点定义，所有Controller下的所有方法
    @Pointcut("execution(* org.csu.mypetstore.controller..*.*(..))")
    public void point(){}

    /**
     * 在连接点方法执行成功后执行
     * 可以拿到结果的返回值
     */
    @AfterReturning(value = "point()",returning = "result")
    public void result(JoinPoint joinPoint, String result){
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
//        System.out.println("方法："+methodName+"被调用，形参是："+Arrays.toString(args)+"，返回值为："+result);
        String msg = "方法："+methodName+"被调用，形参是："+Arrays.toString(args)+"，返回值为："+result;
        logService.insertMessage(msg);
    }

}
